import paginationConf from './pagination';
import _ from 'lodash';
import { addDetectorPushUserById, addNewDetector, deleteDetector, deleteDetectorUser, getDetectorInfoById, getDetectorList, updateDetector } from '@/services/detectorManage';
import { message } from 'antd';

const DetectorModel = {
  namespace: 'detectorManage',
  state: {
    detectors: [], //设备列表
    loading: false,
    selectedRowsKeys: [],
    drawerVisible: false,
    modalVisible: false, //页面是否打开
    visible: false,
    optiontype: null, //打开model操作类型
    detectorDetail: {}, //修改设备信息回显
    deviceDrawerDetail: {}, //Drawer设备信息显示
    effectPageInfo: {},
    pagination: {
      ...paginationConf,
    },
  },
  effects: {
    *getDetectorData({ payload }, { call, put, select }) {
      const pagination = yield select((state) => state.detectorManage.pagination);
      const response = yield call(getDetectorList, { ...payload, pagination });
      const detectorList = _.get(response, 'data.items', []);

      yield put({
        type: 'updateDetectorDataSet',
        payload: {
          detectorList,
          pagination: {
            total: _.get(response, 'data.total', null),
            pageNum: _.get(response, 'data.page', 1),
          },
        },
      });
    },

    *getData({ payload }, { call, put, select }) {
      const pagination = yield select((state) => state.detectorManage.pagination);
      const response = yield call(getDetectorList, { ...payload, pagination });
      const detectorList = _.get(response, 'data.items', []);

      yield put({
        type: 'updateDataSet',
        payload: {
          detectorList,
          pagination: {
            total: _.get(response, 'data.total', null),
            pageNum: _.get(response, 'data.page', 1),
          },
        },
      });
    },
    *submitDetectorInfo({ payload, callback }, { call, put }) {
      const { type, formData } = payload;
      // 判断是更新还是新增
      const callFn = type === 'add' ? addNewDetector : updateDetector;
      const ret = yield call(callFn, formData);
      callback(ret);
    },
    *deleteDetectorInfo({ payload, callback }, { call }) {
      const ret = yield call(deleteDetector, payload)
      callback(ret);
    },

    changePage({ payload }, { put }) {
      put({
        type: 'changePage',
        payload,
      });
    },
    //预留出来，接口联调后优化
    // *updateDrawerDetail({ payload, callback }, { put, call }) {
    //   const ret = yield call(getDetectorInfoById, payload)
    //   debugger
    // }
  },
  reducers: {
    //改变Table表加载状态
    changeLoadingStatus(state, { payload }) {
      return { ...state, loading: !state.loading };
    },

    updateDetectorDataSet(state, { payload }) {
      return {
        ...state,
        visible: !state.visible,
        detectors: payload.detectorList,
        pagination: { ...state.pagination, ...payload.pagination },
        loading: false,
        effectPageInfo: { current: state.pagination.start },
      }
    },
    updateDataSet(state, { payload }) {
      return {
        ...state,
        detectors: payload.detectorList,
        pagination: { ...state.pagination, ...payload.pagination },
        loading: false,
        effectPageInfo: { current: state.pagination.start },
      };
    },
    //打开Model时调用
    changeModalVisibleAndSetValue(state, { payload }) {
      if (payload.type === 'add') {
        return {
          ...state,
          modalVisible: !state.modalVisible,
          detectorDetail: {},
          optiontype: payload.type,
        };
      } else if (payload.type === 'update') {
        return {
          ...state,
          modalVisible: !state.modalVisible,
          optiontype: payload.type,
          detectorDetail:
            {
              ...payload.data,
            } || {},
        };
      }
      return { ...state, modalVisible: !state.modalVisible };
    },
    //关闭Model时调用
    changeModelVisibleAndCloseModel(state, { payload }) {
      return { ...state, modalVisible: !state.modalVisible, optiontype: null, detectorDetail: {} };
    },
    changeModelVisible(state, { payload }) {
      return { ...state, visible: !state.visible };
    },
    changeDrawerStatus(state, { payload }) {
      return { ...state, drawerVisible: !state.drawerVisible, deviceDrawerDetail: payload };
    },
    changePage(state, { payload }) {
      return {
        ...state,
        pagination: { ...state.pagination, start: payload },
      };
    },
  },
};

export default DetectorModel;
